﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace PIM_ATE.Electrical
{
    class clsInstrMps : InstrSwitch
    {
        TestInstMPS m_comObj;
        public string m_sComName;
        bool m_isConnected;
        const string lf = "\r\n";

        public string cmd = "";
        public string result = "";

        public clsInstrMps(int iType)
        {
            m_comObj = new TestInstMPS();
            m_comObj.Enabled = true;
            m_isConnected = false;
            //m_comObj.m_sEnding = (byte)'\n';
            m_iClsType = iType;
        }

        public override bool Connect(string ComName, string path)
        {
            if (GDefine.IS_SWITCH_DEBUG_TEST) return true;

            m_sComName = ComName;
            if (!m_isConnected)
            {
                if (m_comObj.OpenSerialPort(m_sComName) == 0)
                {
                    m_isConnected = true;
                }
                else
                {
                    m_isConnected = false;
                }
            }
            return m_isConnected;
        }

        public override bool Close()
        {
            if (GDefine.IS_SWITCH_DEBUG_TEST) return true;

            if (m_isConnected)
            {
                m_comObj.CloseSerialPort();
                m_isConnected = false;
            }
            return true;
        }

        public override string GetStatus()
        {
            if (GDefine.IS_SWITCH_DEBUG_TEST) return "debug";

            return m_comObj.IDN();
        }

        public override bool Select_Switch_port(int TestPort1, int TestPort2, int Delay)
        {
            if (GDefine.IS_SWITCH_DEBUG_TEST) return true;

            cmd = TestPort1.ToString() + TestPort2.ToString();
            m_comObj.Switch(cmd);
            Thread.Sleep(Delay);

            return true;

            //if (m_comObj.StartWrite(Encoding.ASCII.GetBytes(cmd + lf)))
            //{
            //    result = Encoding.ASCII.GetString(m_comObj.StartRead());
            //    if (result.Contains(cmd))
            //    {
            //        Thread.Sleep(Delay);
            //        return true;
            //    }
            //    m_sbErr.Append("返回错误: "+result);
            //}
            //else
            //{
            //    m_sbErr.Append("接收超时！");
            //}
            //return false;
        }
    }
}
